package mastetAndWorker;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Master {

    protected Queue<Object> workQueue = new ConcurrentLinkedQueue<>();

    protected Map<String, Thread> workerMap = new HashMap<>();

    public Map<String, Object> getResultMap() {
        return resultMap;
    }

    protected Map<String, Object> resultMap = new ConcurrentHashMap<>();

    public Master(Worker worker, int count) {
        worker.setWorkQueue(workQueue);
        worker.setResultMap(resultMap);

        for (int i = 0; i < count ; i++) {
            workerMap.put(Integer.toString(i), new Thread(worker, Integer.toString(i)));
        }
    }

    public void submit(Object o) {
        workQueue.add(o);
    }


    public void execute() {
        workerMap.forEach((k, v)->{
            v.start();
        });
    }





}
